package indi.zhifa.recipe.bailan.busy.entity.po;


import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.*;
import com.querydsl.core.annotations.PropertyType;
import com.querydsl.core.annotations.QueryType;
import indi.zhifa.recipe.bailan.busy.enums.EOrmEntityStatus;
import indi.zhifa.recipe.bailan.framework.common.util.SnowflakeIdWorker;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.UpdateTimestamp;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import javax.persistence.Version;
import java.time.LocalDateTime;
import java.util.Arrays;

@EntityListeners(AuditingEntityListener.class)
@Entity   //表示这个类是一个实体类
@Table(name = "c2_orm_test")
@TableName("c2_orm_test")
@Data
public class OrmTestEntity {


    @Column(name = "id")
    //@GeneratedValue(generator = "orderIdGenerator", strategy = GenerationType.SEQUENCE)
    //@GenericGenerator(name = "orderIdGenerator", strategy = "indi.zhifa.recipe.bailan.busy.handler.SnowIdGenerator")
    @Id
    @Schema(title = "主键")
    @TableId(type = IdType.ASSIGN_ID)
    Long id;
    String appCode;
    String appName;
    String moduleCode;
    String moduleName;
    String code;
    String name;
    EOrmEntityStatus status;
    Integer value;
    String description;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.INSERT)
    @Schema(title = "创建时间")
    @CreatedDate
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @QueryType(PropertyType.NONE)
    LocalDateTime createTime;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.INSERT)
    @Schema(title = "创建用户Id")
    @QueryType(PropertyType.NONE)
    Long createBy;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.INSERT)
    @Schema(title = "创建用户昵称")
    @QueryType(PropertyType.NONE)
    String createName;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.INSERT)
    @Schema(title = "创建者Ip")
    @QueryType(PropertyType.NONE)
    String createIp;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.UPDATE)
    @Schema(title = "修改时间")
    @LastModifiedDate
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @QueryType(PropertyType.NONE)
    LocalDateTime modifyTime;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.UPDATE)
    @Schema(title = "修改用户Id")
    @QueryType(PropertyType.NONE)
    Long modifyBy;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.UPDATE)
    @Schema(title = "修改用户昵称")
    @QueryType(PropertyType.NONE)
    String modifyName;
    @JSONField(serialize = false, deserialize = false)
    @TableField(fill = FieldFill.UPDATE)
    @Schema(title = "创建者Ip")
    @QueryType(PropertyType.NONE)
    String modifyIp;

    @Schema(title = "版本")
    @com.baomidou.mybatisplus.annotation.Version
    @Version
    Integer version;
    @JSONField(serialize = false, deserialize = false)
    @TableLogic
    @Schema(title = "是否删除")
    Boolean del;

    public void initWithCreate(){
        id = SnowflakeIdWorker.generateId();
        createTime = LocalDateTime.now();
        createBy = 0L;
        createName = "sys";
        createIp = "localhost";
        modifyTime = null;
        modifyBy = null;
        modifyName = null;
        modifyIp = null;
        version = 0;
        del = false;
    }
    public Object[] toParamList(){
        return Arrays.asList(
                id, appCode, appName, moduleCode, moduleName, code, name, status.getCode(), description, value,
                createTime, createBy, createName, createIp, modifyTime, modifyBy, modifyName, modifyIp, version, del?1:0).toArray();
    }
}
